<template>
  <el-dialog :title="formData.resourceUuid ? '修改' : '添加'" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="auto" style="padding: 0 20px" class="Dform">
      <el-form-item label="资源名称：" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="资源类型：" prop="resourceType">
        <el-select v-model="formData.resourceType" filterable clearable style="width: 100%" placeholder="请选择品牌">
          <el-option v-for="item in [...resourceTypeMap].map(([value, label]) => ({ value, label }))" :key="item.value"
            :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="资源路径：" prop="resourcePath">
        <el-input v-model="formData.resourcePath" />
      </el-form-item>
      <el-form-item label="前端图标：" prop="icon">
        <icon-select style="width: 100%" v-model:icon="formData.icon" type="element" />
      </el-form-item>
      <el-form-item label="是否显示：">
        <el-radio-group v-model="formData.hidden" v-if="dialog">
          <el-radio :label="0">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button v-if="!formData.resourceUuid" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, PropType, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateResource, saveResource } from '@/api/system'
import { resourceTypeMap } from '@/config/constants'

export default defineComponent({
  components: {
  },
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    },
    selectMenuList: {
      type: Array as PropType<{ title: string; id: string | number }[]>,
      default: () => [],
    },
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    watch(
      () => props.dialog,
      () => {
        Object.assign(formData, props.currentForm)
      }
    )

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          if (formData.resourceUuid) {
            updateResource(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('修改成功')
                close()
              }
            })
          } else {
            saveResource(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('添加成功')
                close()
              }
            })
          }
        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      resourceTypeMap,
      close,
      resetForm,
      onSubmit,
      rules: {
        name: [
          { required: true, message: '请输入前端名称', trigger: 'blur' },
        ],

        icon: [
          { required: true, message: '请输入前端图标', trigger: 'blur' },
          {
            min: 2,
            max: 140,
            message: '长度在 2 到 140 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
})
</script>

<style></style>
